#!/usr/bin/env python
# coding=utf-8

# author: Reggie
# time:   2019/12/19 下午3:57
import os
from glob import iglob
# 提取ts列表文件的内容，逐个拼接ts的url，形成list
from natsort import natsorted
import sys


class TS2MP4(object):
    def __init__(self, root, out_name="out", rm_ts=False):
        self.root = root
        self.out_name = out_name
        self.rm_ts = rm_ts

    def for_windows(self):
        print("开始合并...")
        outdir = "../{}.mp4".format(self.out_name)
        os.chdir(self.root)
        os.system("copy /b *.ts new.mp4")
        os.system("move new.mp4 {}".format(outdir))
        print("结束合并...")

    def for_linux(self):
        out_file_name = os.path.join(self.root, self.out_name + ".mp4")
        ts_path = os.path.join(self.root, '*.ts')
        with open(out_file_name, 'wb') as fn:
            for ts in natsorted(iglob(ts_path)):
                with open(ts, 'rb') as ft:
                    scline = ft.read()
                    fn.write(scline)
        if self.rm_ts:
            for ts in iglob(ts_path):
                os.remove(ts)

    def scheduling(self):
        self.for_linux()
        # platform = sys.platform
        # print(platform.center(100, "*"))
        # if platform in ["win32"]:
        #     self.for_windows()
        # elif platform in ["linux", "darwin"]:
        #     self.for_linux()
        # else:
        #     raise Exception("Un Support System Type")